package top.alanshelby.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.alanshelby.entity.Item;
import top.alanshelby.exception.CustomException;
import top.alanshelby.service.ItemService;

import java.util.List;

@Controller
@RequestMapping(value = "exception")
public class ExceptionTestController {

    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "queryItem")
    public String queryGoods(Model model) throws CustomException {
        List<Item> itemList = itemService.findItemList();
        // 抛出一个自定义异常
        if (itemList.size() < 10) {
            throw new CustomException("这是一个自定义异常！");
        }
        model.addAttribute("itemList", itemList);
        return "itemList";
    }
}
